{extends 'admin/layout/table-main.tpl'}

{block title}管理员管理{/block}
{block smallTitle}系统内的管理员管理。{/block}
{block head}
	<link href="{asset('admin/css/plugins/chosen/chosen.css')}" rel="stylesheet">
{/block}
{block main}
<div class="row">
	<div class="col-sm-12">
		<div class="ibox float-e-margins">
			<div class="ibox-title">
				<h5>管理员管理 <small>后台管理员编辑/添加</small></h5>
			</div>
			<div class="ibox-content">
				<form method="post" class="form-horizontal" action="{route('AdminAdminEditAction')}">
					{if $data}
					<div class="form-group">
						<label class="col-sm-2 control-label">#</label>
						<div class="col-sm-10">
							<p class="form-control-static">{$data.id}</p>
						</div>
					</div>
					{/if}
					<div class="hr-line-dashed"></div>
					<div class="form-group">
						<label class="col-sm-2 control-label">用户名</label>
						<div class="col-sm-10">
							<input type="text" class="form-control" name="username" value="{Input::old('username', $data.username)|escape}">
						</div>
					</div>
					<div class="hr-line-dashed"></div>
					<div class="form-group">
						<label class="col-sm-2 col-sm-2 control-label">头像</label>
						<div class="col-sm-10">
							<div class="fileupload fileupload-new" data-provides="fileupload"
								 id="upload_picture">
								<input type="hidden" name="avatar_id"
									   value="{Input::old('avatar_id')|default:$data.avatar_id}" />
								<div class="fileupload-new thumbnail"
									 style="width: 120px; height: 120px;">
									{if $data.avatar_id}<img
										src="{route('FilePull', ['id'=>$data.avatar_id])}">{else}<img
											src="http://www.placehold.it/150x150/EFEFEF/AAAAAA&amp;text=no+image"
											alt="" />{/if}
								</div>
								<div class="fileupload-preview fileupload-exists thumbnail"
									 style="max-width: 120px; max-height: 120px; line-height: 20px;"></div>
								<div class="actions">
									<span class="btn btn-file"><button class="btn btn-default fileupload-new" type="button"><i class="fa fa-cloud-upload"></i>选择图片</button>
										<span class="fileupload-exists">更改图片</span>
										 <input type="file" class="default upload_pic" /> </span>
									<a href="#" class="btn close" data-dismiss="fileupload" style="display: none;">删除</a>
								</div>
							</div>
							<span class="label label-danger">注意</span> <span>图片上传只支持最新的Firefox、Chrome、Opera、Safari和Internet
								Explorer 10 以上版本的浏览器。</span>
						</div>
					</div>
					<div class="hr-line-dashed"></div>
					<div class="form-group">
						<label class="col-sm-2 col-sm-2 control-label">角色</label>
						<div class="col-sm-10">
							<select data-placeholder="选择角色" multiple id="my_multi_select1" name="roles[]" class="chosen-select" style="width:350px;" tabindex="4">
								{foreach $roles as $role}
									{$selected = ''}
									{foreach $data.roles as $item}
										{if $item.id eq $role.id} {$selected = 'selected'}
											{break}
										{/if}
									{/foreach}
									<option value="{$role.id}"{$selected}>{$role.name}</option>
								{/foreach}
							</select>
						</div>
					</div>
					<div class="hr-line-dashed"></div>
					<div class="form-group">
						<label class="col-sm-2 col-sm-2 control-label">渠道</label>
						<div class="col-sm-10">
							<button class="btn btn-white " type="button" id="openedit" data-url="{route('AdminSourceGetAll')}">
								<i class="fa  fa-bar-chart"></i>
								渠道
							</button>
							<span id="citybox">
									{foreach $channel as $item}
										<a href="javascript:;" id="city_{$item.id}"> {$item.name} <input type="hidden" name="channel_ids[]" value="{$item.id}"></a>
									{/foreach}
								</span>
						</div>
					</div>
					<div class="hr-line-dashed"></div>
					<div class="form-group">
						<label class="col-sm-2 col-sm-2 control-label">密码</label>
						<div class="col-sm-10">
							<input type="password" class="form-control" name="password">
							{if $data}<p class="help-block">不修改密码请留空。</p>{/if}
						</div>
					</div>
					<div class="hr-line-dashed"></div>
					<div class="form-group">
						<label class="col-sm-2 control-label">确认密码</label>
						<div class="col-sm-10">
							<input type="password" class="form-control" name="password_confirmation">
						</div>
					</div>
					<div class="hr-line-dashed"></div>
					<div class="form-group">
						<label class="col-sm-2 control-label">姓名</label>
						<div class="col-sm-10">
							<input type="text" class="form-control" name="realname" value="{Input::old('realname', $data.realname)|escape}">
						</div>
					</div>
					<div class="hr-line-dashed"></div>
					<div class="form-group">
						<div class="col-sm-4 col-sm-offset-2">
							<input type="hidden" class="form-control" name="id" value="{$data.id}">
							<button class="btn btn-primary" type="submit">保存内容</button>
							<a class="btn btn-white" type="button" href="{route('AdminAdminList')}">返回</a>
						</div>
					</div>
				</form>
			</div>
		</div>
	</div>
</div>
{/block}

{block script}
	<script src="{asset('admin/js/plugins/layer/layer.min.js')}"></script>
	<script>
		$(document).ready(function(){
			$('#openedit').click(function(){
				var url = $(this).attr('data-url');
				layer.open({
					type: 2,
					title: false,
					shadeClose: false,
					shade: 0.8,
					area: ['800px', '400px'],
					content: url
				});
			});
		})
	</script>
	<link href="{asset('admin/css/bootstrap-fileupload.min.css')}" rel="stylesheet" type="text/css" />
	<script src="{asset('admin/js/bootstrap-fileupload.min.js')}" type="text/javascript" ></script>
	<script>

		// 文件上传。
		$.fn.eduFileUpload = function(options){
			options = $.extend({
				name : null
			}, options || {});
			function formatSize(size) {
				var unit = ['B', 'KB', 'MB', 'GB', 'TB', 'DB'];
				var i = arguments[1];
				if (typeof arguments[1] == 'undefined') {
					i = 0;
				}
				if (size < 1024) {
					return size.toFixed(2)+unit[i];
				} else {
					return formatSize(size/1024, ++i);
				}
			}
			this.each(function() {
				var $this = $(this);
				var xhr_before = null;

				var $statistics = $('<span class="statistics" style="display: none;">\
                <span class="loaded">0M</span> /\
                <span class="file_size">0M</span>\
                <span class="upload_progress">0%</span>\
            </span>').appendTo($this);

				var $file = $this.find(':file:first');
				var $input = options.name ? $this.find('input[type="hidden"][name="' + options.name + '"]') : $this.find('input[type="hidden"]:first');

				$this.find('.close').on('click', function(){
					$file.val('');
					$input.val('');
					if (xhr_before){
						xhr_before.abort();
						xhr_before = null;
					}
					$this.data('uploading', false);
				});

				$this.data('uploading', false);
				$file.on('change', function(){
					if( ! this.files[0]) return;
					$this.data('uploading', true);
					var data = new FormData();
					data.append('body', this.files[0]);
					$.ajax({
						type        : 'POST',
						url         : '{route('FileUpload')}',
						data        : data,
						processData : false,
						contentType : false,
						beforeSend  : function(xhr){
							if (xhr_before) xhr_before.abort();
							xhr_before = xhr;
							xhr.progress = function(e) {
								if (e.lengthComputable) {
									var percentComplete = Math.round(e.loaded * 100 / e.total);
									$statistics.find('.upload_progress').html(percentComplete+'%');
									$statistics.find('.loaded').html(formatSize(e.loaded));
								} else {
									$statistics.find('.loaded').html('上传中');
									$statistics.find('.upload_progress').html('');
								}
							};
						},
						success     : function(json){
							xhr_before = null;
							$this.data('uploading', false);
							if(json.mime.substring(0,6) == 'image/'){
								$input.val(json.file_id);
							}else{
								$input.val('');
								$this.find('.close').trigger('click');
								ialert('不支持的图片格式。');
							}
						},
						error       : function (xhr) {
							var message = xhr.responseText;
							xhr_before = null;
							$this.data('uploading', false);
							$this.find('.close').trigger('click');
						}
					});
				});
			});
		};

		$('#upload_picture').eduFileUpload({ name : 'avatar_id' });

	</script>
	<script src="{asset('admin/js/plugins/chosen/chosen.jquery.js')}"></script>
	<script src="{asset('admin/js/demo/form-advanced-demo.min.js')}"></script>

{/block}
